<template>
    <el-dialog
        v-model="showflag"
        :title="title"
        width="50vw"
        align-center
        :close-on-click-modal="false"
        @close="resetdata">
        <div style="padding: 0 40px">
            <el-form
                ref="ruleFormRef"
                style="width: 100%"
                :model="ruleForm"
                :rules="rules"
                label-width="auto"
                class="demo-ruleForm"
                :size="formSize"
                status-icon>
                <div class="linebox">
                    <el-form-item label="利用单位名称" prop="name">
                        <el-input
                            v-model="ruleForm.name"
                            :disabled="!isedit"
                            placeholder="请输入利用单位名称" />
                    </el-form-item>
                    <el-form-item label="单位地址" prop="address">
                        <el-input
                            v-model="ruleForm.address"
                            :disabled="!isedit"
                            placeholder="请输入单位地址" />
                    </el-form-item>
                </div>
                <div class="linebox">
                    <el-form-item label="所属省份" prop="province">
                        <el-select
                            @change="changprovince"
                            v-model="ruleForm.province"
                            placeholder="请选择"
                            :disabled="!isedit">
                            <el-option
                                v-for="(item, index) in provincelist"
                                :key="index"
                                :label="item.name"
                                :value="item.name" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="所属市" prop="city">
                        <el-select
                            @change="changcity"
                            v-model="ruleForm.city"
                            placeholder="请选择"
                            :disabled="!isedit">
                            <el-option
                                v-for="(item, index) in citylist"
                                :key="index"
                                :label="item.name"
                                :value="item.name" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="所属区县" prop="area">
                        <el-select
                            v-model="ruleForm.area"
                            placeholder="请选择"
                            :disabled="!isedit">
                            <el-option
                                v-for="(item, index) in arealist"
                                :key="index"
                                :label="item.name"
                                :value="item.name" />
                        </el-select>
                    </el-form-item>
                    
                    
                </div>
                <div class="linebox">
                    <el-form-item label="联系人" prop="uname">
                        <el-input
                            v-model="ruleForm.uname"
                            :disabled="!isedit"
                            placeholder="请输入联系人" />
                    </el-form-item>
                    <el-form-item label="联系电话" prop="telphone">
                        <el-input
                            v-model="ruleForm.telphone"
                            :disabled="!isedit"
                            placeholder="请输入联系电话" />
                    </el-form-item>
                </div>
                <div class="linebox">
                    <el-form-item label="排序" prop="sort">
                        <el-input
                            v-model="ruleForm.sort"
                            :disabled="!isedit"
                        />
                    </el-form-item>
                </div>
               
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="showflag = false">取消</el-button>
                <el-button
                    v-if="isedit"
                    type="primary"
                    @click="submitForm(ruleFormRef)">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { onMounted, ref, provide, reactive, toRaw, inject } from "vue";
import { ElMessage } from "element-plus";
import { post, baseURL } from "../../../api/request";
import type { FormInstance, FormRules } from "element-plus";

let showflag = ref(false);
let title = ref("新增利用单位");
let isedit = ref(true);
let device_id = ref("");
let token = ref("");
const getdata = inject<() => void>("getdata")!; //父组件的方法
let citylist = ref([]);
let arealist = ref([]);
let provincelist = ref([]);

onMounted(() => {
    token.value = localStorage.getItem("token") || "";

    post({
        url: "/api/area/index",
        data: {
            parent_id: '',
        },
    }).then((res: any) => {
        provincelist.value = res.data.list;
    });


    post({
        url: "/api/area/index",
        data: {
            parent_id: 3021,
        },
    }).then((res: any) => {
        citylist.value = res.data.list;
    });
});

const changprovince = (e: any) => {
    let id = provincelist.value.find((item: any) => item.name == e)?.id;
    post({
        url: "/api/area/index",
        data: {
            parent_id: id,
        },
    }).then((res: any) => {
        citylist.value = res.data.list;
        arealist.value = [];
        ruleForm.area = ''
        ruleForm.city = ''
    });
};

const changcity = (e: any) => {
    let id = citylist.value.find((item: any) => item.name == e)?.id;
    post({
        url: "/api/area/index",
        data: {
            parent_id: id,
        },
    }).then((res: any) => {
        arealist.value = res.data.list;
        ruleForm.area = ''
    });
};

//表单相关---------------------------------------
interface RuleForm {
    id: string;
    name: string;
    address:string;
    province:string;
    city: string;
    area: string;
    uname: string;
    telphone: string;
    sort:number;
}

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
let ruleForm = reactive<RuleForm>({
    id: "",
    name: "",
    address: "",
    province: "青海省",
    city: "",
    area: "",
    uname: "",
    telphone: "",
    sort: 0
});

const rules = reactive<FormRules<RuleForm>>({
    name: [{ required: true, message: "请填写公司名称", trigger: "blur" }],
    address: [{ required: true, message: "请填写单位地址", trigger: "blur" }],
    province: [{ required: true, message: "请选择所属省", trigger: "blur" }],
    city: [{ required: true, message: "请选择所属市", trigger: "blur" }],
    area: [{ required: true, message: "请选择所属区县", trigger: "blur" }],
    uname: [{ required: true, message: "请填写联系人", trigger: "blur" }],  
    telphone: [{ required: true, message: "请填写联系电话", trigger: "blur" }]
});

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((valid, fields) => {
        if (valid) {
            post({
                url: ruleForm.id
                    ? "/api/operate/edit"
                    : "/api/operate/add",
                data: toRaw(ruleForm),
            }).then((res: any) => {
                ElMessage.success("操作成功!");
                getdata();
                showflag.value = false;
                resetdata();
            });
        } else {
            console.log("error submit!");
        }
    });
};
//表单END---------------------------------------



const resetdata = () => {
    Object.assign(ruleForm, {
        id: "",
        name: "",
        address: "",
        province: "青海省",
        city: "",
        area: "",
        uname: "",
        telphone: "",
        sort: 0
    });
    isedit.value = true;
    title.value = "新增利用单位";
};

// 通过 defineExpose 暴露给父组件
defineExpose({
    showflag,
    title,
    isedit,
    device_id,
    ruleForm,
});
</script>

<style lang="scss" scoped>
:deep(.el-form-item) {
    margin-bottom: 30px;
    width: 50%;
}
.linebox {
    display: flex;
    align-items: flex-start;
    width: 100%;
}
:deep(.avatar-uploader .avatar) {
    width: 178px;
    height: 178px;
    display: block;
}
:deep(.avatar-uploader .el-upload) {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

:deep(.avatar-uploader .el-upload:hover) {
    border-color: var(--el-color-primary);
}

:deep(.el-icon.avatar-uploader-icon) {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>
